import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/server/ws/ali_im_manager.dart';

class AliImTestController extends GetxController {
  ScrollController scrollController = ScrollController();

  void toEnd() {
    scrollController.animateTo(scrollController.position.maxScrollExtent,
        duration: const Duration(milliseconds: 200), curve: Curves.easeIn);
  }
}

class AliImTestPage extends StatefulWidget {
  const AliImTestPage({super.key});

  @override
  State<AliImTestPage> createState() => _AliImTestPageState();
}

class _AliImTestPageState extends State<AliImTestPage> {
  List<String> message = [];
  ScrollController scrollController = ScrollController();
  AliImTestController logic = Get.put(AliImTestController());

  // @override
  // void initState() {
  //   AliImManager.messageStream.listen((event) {
  //     setState(() {
  //       message.add("${DateTime.now().formatedString("HH:mm:ss.SSS")} ${jsonEncode(event)}");
  //     });
  //   });
  //   super.initState();
  // }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AliImTestController>(
      builder: (GetxController controller) => Scaffold(
        appBar: Styles.appBar(title: const Text("AliIm测试")),
        body: ListView.builder(
          controller: logic.scrollController,
          itemCount: AliImManager.record.length,
          itemBuilder: (BuildContext context, int index) {
            return Text(AliImManager.record[index]);
          },
        ),
      ),
    );
  }
}
